package com.yunao.dao;

import com.yunao.entity.Report;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface ReportDao extends CrudRepository<Report,Integer> {

    List<Report> findAllByUserIdAndMannerAndAloneReportId(Integer userId, Integer manner, Integer aloneReportId, Pageable pageable);

    List<Report> findAllByNameAndUserId(String name, Integer userId);

    List<Report> findAllByNameAndAddrs(String name, String addrs);

    @Transactional
    @Query(value = "SELECT * FROM report WHERE (manner = 1 OR manner = 2 OR manner = 4) AND alone_report_id = :aloneReportId AND user_id = :userId ORDER BY id DESC",nativeQuery= true)
    List<Report> findAllByAloneReportIdAndUserId(@Param("aloneReportId") String aloneReportId, @Param("userId") String userId, Pageable pageable);

    Long countByMannerAndAloneReportIdAndUserId(Integer manner, Integer aloneReportId, Integer userId);

}
